<?php
namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use App\Http\Requests\TagRequest;
use App\Http\Resources\Tag;
use App\Http\Resources\TagCollection;
use App\Repositories\TagRepository;
use App\Scopes\StatusScope;
use App\Tag as TagModel;
use App\Traits\BaseApiFunc;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;

class TagController extends BaseController {

	use BaseApiFunc;

	protected $model, $page_size;

	public function __construct(TagRepository $tag) {
		parent::__construct();

		$this->model = $tag;
		$this->page_size = config('blog.admin_page.common_page');
	}

	public function store(TagRequest $request) {
		$this->model->store($request->all());
		return $this->response->withNoContent();
	}

	public function all() {
		$list = $this->model->select('id', 'tag_name', 'status')->get();
		return $this->response->withSuccess($list);
	}

	public function tags() {
		$list = Cache::remember('tags', config('blog.cache.time'), function () {
			return $this->model->select('id', 'tag_name')->get();
		});
		return $this->response->withSuccess($list);
	}

	protected function getResourceCollection(Collection $collection) {
		return new TagCollection($collection->load(['items' => function ($query) {
			return $query->withoutGlobalScope(StatusScope::class);
		}]));
	}

	protected function getResource(TagModel $collection) {
		return new Tag($collection);
	}
}
